import 'package:flutter_admin/api/odoo/sale_pigtail_product_plan_odoo_api.dart';
import 'package:flutter_admin/models/odoo/product_template.dart';
import 'package:flutter_admin/models/odoo/sale_pigtail_product_plan.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';

import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/udt_tb_workreportrecord_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:get_storage/get_storage.dart';

class UdtTbWorkreportrecordProductSelect extends StatefulWidget {

  final String? teamId;

  final String? processId;

  final bool? isMOP;

  UdtTbWorkreportrecordProductSelect({Key? key,required this.teamId,required this.processId,required this.isMOP}):super(key: key);

  @override
  _UdtTbWorkreportrecordProductSelectState createState() =>
      _UdtTbWorkreportrecordProductSelectState();
}

class _UdtTbWorkreportrecordProductSelectState
    extends State<UdtTbWorkreportrecordProductSelect> {
  SalePigtailProductPlan salePigtailProductPlan = SalePigtailProductPlan();
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  PageModel page = PageModel(size: 30,orders: [OrderItemModel(column: 'create_date', asc: false)]);
  bool flag = true;
  @override
  void initState() {
    super.initState();
    if (StoreUtil.hasData("warehousingTime")){
      salePigtailProductPlan.warehousingTime = StoreUtil.read("warehousingTime");
    }else{
      salePigtailProductPlan.warehousingTime = Utils.getDate();
    }
    salePigtailProductPlan.team = widget.teamId;
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi =
    await SalePigtailProductPlanOdooApi.page(data: {
      "page": page,
      "domainList": salePigtailProductPlan.toDomainList(processId: widget.processId),
    });
    if (responseBodyApi.success!) {
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
      StoreUtil.write("warehousingTime", salePigtailProductPlan.warehousingTime);
    }
  }

  _query() {
    formKey.currentState?.save();
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _reset() {
    salePigtailProductPlan = SalePigtailProductPlan();
    if (StoreUtil.hasData("warehousingTime")){
      salePigtailProductPlan.warehousingTime = StoreUtil.read("warehousingTime");
    }else{
      salePigtailProductPlan.warehousingTime = Utils.getDate();
    }
    salePigtailProductPlan.team = widget.teamId;
    formKey.currentState!.reset();
    _loadData();
  }



  @override
  Widget build(BuildContext context) {
    List<DataColumn> _getColumns(){
      List<DataColumn> columns = [
        DataColumn(label: Text("状态")),
        DataColumn(label: Text('缆径')),
        DataColumn(label: Text('米数')),
        DataColumn(label: Text('未完成数量')),
        DataColumn(label: Text('产品名称')),
        DataColumn(label: Text('规格型号')),
        DataColumn(label: Text('芯数')),
        DataColumn(label: Text('数量')),
        DataColumn(label: Text('日期')),
        DataColumn(label: Text('生产单号')),
        DataColumn(label: Text('客户')),
      ];
      if(widget.isMOP!){
        columns = [
          DataColumn(label: Text("状态")),
          DataColumn(label: Text("未完成数量")),
          DataColumn(label: Text('产品名称')),
          DataColumn(label: Text('规格型号')),
          DataColumn(label: Text('芯数')),
          DataColumn(label: Text('数量')),
          DataColumn(label: Text('日期')),
          DataColumn(label: Text('生产单号')),
          DataColumn(label: Text('客户')),
        ];
      }
      return columns;
    }

    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            width: 280,
            label: '日期',
            value: salePigtailProductPlan.warehousingTime,
            onSaved: (v) {
              salePigtailProductPlan.warehousingTime = v;
            },
          ),
          CryInput(
            width: 280,
            label: '规格型号',
            value: salePigtailProductPlan.pattern,
            onSaved: (v) {
              salePigtailProductPlan.pattern = v;
            },
          ),
        ],
      ),
    );

    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      columnSpacing: 20,
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        if (flag) {
          Navigator.pop(context, selected);
        }
        flag = false;
      },
      columns: _getColumns(),
      getCells: (m) {
        SalePigtailProductPlan salePigtailProductPlan =
        SalePigtailProductPlan.fromMap(m);
        List<DataCell> cells = [
          DataCell(salePigtailProductPlan.condition == '0'? Text("未开始",style: TextStyle(color: Colors.purple),):
                   salePigtailProductPlan.condition == '1'? Text("进行中",style: TextStyle(color: Colors.orangeAccent),):
                   salePigtailProductPlan.condition == '2'? Text("已完成",style: TextStyle(color: Colors.greenAccent),):
                   Text("--")
          ),
          DataCell(Text(salePigtailProductPlan.cableDiameter ?? '--')),
          DataCell(Text(salePigtailProductPlan.meters ?? '--')),
          DataCell(Text(salePigtailProductPlan.marginQuantity ?? '--')),
          DataCell(Text(salePigtailProductPlan.productName ?? '--')),
          DataCell(Text(salePigtailProductPlan.pattern ?? '--')),
          DataCell(Text(salePigtailProductPlan.coreNumber ?? '--')),
          DataCell(Text(salePigtailProductPlan.quantity ?? '--')),
          DataCell(Text(salePigtailProductPlan.warehousingTime ?? '--')),
          DataCell(Text(salePigtailProductPlan.orderNumber ?? '--')),
          DataCell(Text(salePigtailProductPlan.clientele ?? '--')),
        ];
        if(widget.isMOP!){
          cells = [
            DataCell(salePigtailProductPlan.condition == '0'? Text("未开始",style: TextStyle(color: Colors.purple),):
                      salePigtailProductPlan.condition == '1'? Text("进行中",style: TextStyle(color: Colors.orangeAccent),):
                      salePigtailProductPlan.condition == '2'? Text("已完成",style: TextStyle(color: Colors.greenAccent),):
                      Text("--")
            ),
            DataCell(Text(salePigtailProductPlan.marginQuantity ?? '--')),
            DataCell(Text(salePigtailProductPlan.productName ?? '--')),
            DataCell(Text(salePigtailProductPlan.pattern ?? '--')),
            DataCell(Text(salePigtailProductPlan.coreNumber ?? '--')),
            DataCell(Text(salePigtailProductPlan.quantity ?? '--')),
            DataCell(Text(salePigtailProductPlan.warehousingTime ?? '--')),
            DataCell(Text(salePigtailProductPlan.orderNumber ?? '--')),
            DataCell(Text(salePigtailProductPlan.clientele ?? '--')),
          ];
        }
        return cells;
      },
    );

    CryButtonBar buttonBar = CryButtonBar(
      children: <Widget>[
        CryButtons.query(context, () => _query()),
        CryButtons.reset(context, () => _reset()),
      ],
    );

    var result = Scaffold(
      appBar: AppBar(
        // leading: Builder(
        //   builder: (BuildContext context) {
        //     return SizedBox.shrink();
        //   },
        // ),
        title: Text("选择产品"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          form,
          buttonBar,
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
